package com.weic.easycache.user;

import java.util.Date;

import com.j256.ormlite.field.DataType;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;
import com.weic.easycache.framework.user.IUser;

import android.os.Parcel;
import android.os.Parcelable;

@DatabaseTable
public class User implements IUser {
	public static final String FIELD_USER_NAME	= "username";
	public static final String FIELD_DATE		= "date";
	
	public static final long NO_ID = -1;
	
	/**接口返回的id。默认为-1，表示没有用户信息，则不允许某些与用户相关的操作如收藏和购物车**/
	@DatabaseField(id = true)
	public final long id;
	@DatabaseField(columnName = FIELD_USER_NAME)
	public final String name;
	@DatabaseField
	public String pwd;

	@DatabaseField
	public final String passport;

	/**头像地址**/
	@DatabaseField
	public String logoUrl;

	@DatabaseField
	public float coins;
	@DatabaseField
	public float score;

	@DatabaseField
	public String email;
	/**手机号码，可能带有字符如+、-等**/
	@DatabaseField
	public String phone;
	/**测试证明：java.sql.Date是不被支持的，即使支持，也只能精确到日，而不能精确到毫秒**/
	@DatabaseField(columnName = FIELD_DATE, dataType = DataType.DATE_STRING)
	public Date date;

	public final boolean logined;
	
	public UserConfig config;
	
	public User() {
		this.id = NO_ID;
		this.name = null;
		this.passport = null;
		this.logined = false;
	}

	public User(long id, String userName, String passport, boolean logined) {
		this.id = id;
		this.name = userName;
		this.passport = passport;
		this.logined = logined;
	}

	@SuppressWarnings("unchecked")
	@Override
	public User invalidate() {
		User user = new User(NO_ID, name, null, false);
		user.copyFromLocal(this);
		user.copyFromLogin(this);
		user.logoUrl = null;
		return user;
	}

	@SuppressWarnings("unchecked")
	@Override
	public User logout() {
		return new User(NO_ID, name, null, false);
	}
	
	@Override
	public boolean isLogined() {
		return logined;
	}

	@Override
	public long getUserId() {
		return id;
	}
	
	@Override
	public String getPassport() {
		return passport;
	}

	public User copyFromLogin(User info) {
		coins = info.coins;
		score = info.score;
		logoUrl = info.logoUrl;
		return this;
	}

	public User copyFromLocal(User user) {
		email = user.email;
		phone = user.phone;
		return this;
	}

	@Override
	public int hashCode() {
		return (int)id;
	}

	@Override
	public boolean equals(Object o) {
		User user = (User)o;
		return user.id==id && user.logined==logined
				&& (user.name==name || user.name.equals(name));
	}

	@Override
	public String toString() {
		return "[id:"+id+", userName:"+name+
				",\n email:"+email+", phone:"+phone+
				",\n balance:"+coins+", score:"+score+
				",\n logoUrl:"+logoUrl+
				",\n logined:"+logined+
				",\n passport:"+passport+"]";
	}

	@Override
	public int describeContents() {
		return (int)id;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeLong(id);
		dest.writeString(name);
		dest.writeString(passport);
		dest.writeString(logoUrl);
		dest.writeFloat(coins);
		dest.writeFloat(score);
		dest.writeString(email);
		dest.writeString(phone);
		dest.writeInt(logined ? 1 : 0);
	}

	public static final Parcelable.Creator<User> CREATOR = new Parcelable.Creator<User>() {
		public User createFromParcel(Parcel in) {
			return new User(in);
		}

		public User[] newArray(int size) {
			return new User[size];
		}
	};

	private User(Parcel in) {
		id = in.readLong();
		name = in.readString();
		passport = in.readString();
		logoUrl = in.readString();
		coins = in.readFloat();
		score = in.readFloat();
		email = in.readString();
		phone = in.readString();
		logined = in.readInt() != 0;
	}
}
